<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "gky_material".
 *
 * @property string $id
 * @property string $materialno
 * @property string $materialtype
 * @property string $materialname
 * @property string $summary
 * @property string $updatetime
 * @property integer $isfinish
 * @property string $addman
 * @property string $addmanid
 * @property string $addtime
 * 
 * * @property MaterialUpfile[] $materialupfiles
 */
class Material extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'gky_material';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [

            [['isfinish', 'addmanid'], 'integer'],
            [['materialno', 'materialtype', 'materialname', 'addman'], 'string', 'max' => 64],
            [['summary'], 'string', 'max' => 2048],
            //数据校验
            [['materialno', 'materialtype', 'materialname'], 'required'],
            //添加校验
            [['updatetime', 'addtime'], 'safe']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'materialno' => '材料编号',
            'materialtype' => '材料类型',
            'materialname' => '材料名称',
            'summary' => '概述',
            'updatetime' => '更新时间',
            'isfinish' => '是否完成',
            'addman' => '添加人',
            'addmanid' => '添加人id',
            'addtime' => '添加时间',
        ];
    }

    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            //添加修改人的信息 由于这部分没有必要自己填写 所以直接获取登录人的信息  在保存的时候设置就好
            $id = Yii::$app->user->identity->id;
            //添加人的id
            $this->addmanid = $id;
            //添加人的姓名
            $name = GkyUser::find()->where(['id' => $id])->one()->username;
            $this->addman = $name;
            //更新时间
            $time = date("Y-m-d");
            $this->updatetime = $time;
            //添加时间
            $this->addtime = $time;
            return true;
        } else {
            return false;
        }
    }
    
    
    public function afterSave($insert, $changedAttributes) {
        parent::afterSave($insert, $changedAttributes);

//        $upfile = new Upfile();
        $upfile = new MaterialUpfile();
        // $upfile->addtime=  date("y-m-d h:i:s",time());
        $path = Yii::$app->request->post('files');
        $filename = Yii::$app->request->post('fileNames');
        $filestatus = Yii::$app->request->post('fileStatus');
        for ($i = 0; $i < count($filestatus); $i++) {
            if ($upfile->save()) {
                $upfile->activeid = $this->id;
                if ($filestatus[$i] == 1) {
                    $upfile->path = $path[$i];
                    $upfile->filename = $filename[$i];
                    $upfile->save();
                    $upfile = new Upfile();
                    //  $upfile->addtime=  date("y-m-d h:i:s",time());
                }
            }
        }
    }
    
    
     public function getUpfiles() {

        return $this->hasMany(MaterialUpfile::className(), ['activeid' => 'id']);
    }

}
